package com.zz.source.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.source.entity.User;
import com.zz.source.mapper.UserMapper;
import com.zz.source.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenglei
 * @since 2021-08-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * @Date: 14:59 2021/8/15
     * @Param: [name, sex]
     * @return: java.util.List<com.zz.source.entity.User>
     * @Description: 根据姓名和性别查询
     **/
    @Override
    public List<User> findNameAndSex(String name, String sex) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name", name)
                        .eq("sex", sex);
        return userMapper.selectList(userQueryWrapper);
    }

    /**
     * @Date: 15:12 2021/8/15
     * @Param: [id, name]
     * @return: int
     * @Description: 根据id修改姓名
     **/
    @Override
    public int updateSetNameAndById(Integer id, String name) {
        User user = new User();
        user.setName(name);

        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("id", id);

        return userMapper.update(user,userQueryWrapper);
    }


    /**
     * @Date: 15:21 2021/8/15
     * @Param: [id, name, username, sex, age]
     * @return: int
     * @Description: 新增用户
     **/
    @Override
    public int insertUser(Integer id, String name, String username, String sex, Integer age) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setUsername(username);
        user.setSex(sex);
        user.setAge(age);

        return userMapper.insert(user);
    }

    /**
     * @Date: 15:34 2021/8/15
     * @Param: [id]
     * @return: int
     * @Description: 根据id删除用户
     **/
    @Override
    public int deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    /**
     * @Date: 15:40 2021/8/15
     * @Param: []
     * @return: java.util.List<com.zz.source.entity.User>
     * @Description: 查询所有用户的名字的性别
     **/
    @Override
    public List<User> findAllNameAndSex() {
        return userMapper.findAllNameAndSex();
    }

    /**
     * @Date: 9:59 2021/8/17
     * @Param: [current, size]
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.zz.source.entity.User>
     * @Description: 分页查询
     **/
    @Override
    public IPage<User> findAllByPage(Integer current, Integer size) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByAsc("id");

        IPage<User> userPage = new Page<>(current, size);

        return userMapper.selectPage(userPage,userQueryWrapper);
    }


}
